package com.rk.live.activity;

import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.rk.live.slidingmenu.SlidingMenu;
import cn.waps.AppConnect;

import com.rk.live.R;
import com.rk.live.activity.base.BaseSlidingFragmentActivity;
import com.rk.live.adapter.BehindListAdapter;
import com.rk.live.uitl.LogUtil;
import com.rk.live.widget.CustomButton;

/**
 * 主页面
 * 
 * @author hui.zhu
 * 
 */
public class MainActivity extends BaseSlidingFragmentActivity implements
		OnClickListener {
	private SlidingMenu sm;
	// above title
	private LinearLayout llGoHome;
	private ImageView imgMore;
	private TextView mAboveTitle;
	// above text
	private TextView mText;
	// above load
	private LinearLayout loadLayout;
	private LinearLayout loadFaillayout;
	// behind list
	private ListView lvTitle;
	// behind button
	private ImageButton imgLogin;
	private CustomButton cbFeedback;
	private CustomButton cbAbove;

	private List<Object> categoryList;
	private BehindListAdapter behindListAdapter;
	private String current_page;
	private boolean isShowPopupWindows = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSlidingMenu();
		setContentView(R.layout.above_slidingmenu);
		initOffer();
		initView();
		initListView();
		// initNav();
	}

	private void initOffer() {
		AppConnect.getInstance(this);
		// 使用自定义的OffersWebView
		AppConnect.getInstance(this).setAdViewClassName(
				"cn.waps.demo188.MyAdView");
		// 禁用错误报告
		AppConnect.getInstance(this).setCrashReport(false);
		// 初始化自定义广告数据
		AppConnect.getInstance(this).initAdInfo();
		// 初始化插屏广告数据
		AppConnect.getInstance(this).initPopAd(this);
//		// 互动广告调用方式
//		LinearLayout container = (LinearLayout) findViewById(R.id.AdLinearLayout);
//		new AdView(this, container).DisplayAd();
//		// 迷你广告调用方式
//		// AppConnect.getInstance(this).setAdBackColor(Color.argb(50, 120, 240,
//		// 120));//设置迷你广告背景颜色
//		// AppConnect.getInstance(this).setAdForeColor(Color.YELLOW);//设置迷你广告文字颜色
//		LinearLayout miniLayout = (LinearLayout) findViewById(R.id.miniAdLinearLayout);
//		new MiniAdView(this, miniLayout).DisplayAd(10);// 10秒刷新一
	}

	// [start]初始化函数
	private void initSlidingMenu() {
		setBehindContentView(R.layout.behind_slidingmenu);
		// customize the SlidingMenu
		sm = getSlidingMenu();
		// 两页面间的阴影width
		sm.setShadowWidthRes(R.dimen.shadow_width);
		// 显示低页时上页保留width
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// 两页面间的阴影图片
		sm.setShadowDrawable(R.drawable.slidingmenu_shadow);
		// sm.setShadowWidth(20);
		sm.setBehindScrollScale(0);
	}

	private void initView() {
		// load加载提示
		loadLayout = (LinearLayout) findViewById(R.id.view_loading);
		loadFaillayout = (LinearLayout) findViewById(R.id.view_load_fail);
		// title
		llGoHome = (LinearLayout) findViewById(R.id.Linear_above_toHome);
		llGoHome.setOnClickListener(this);
		mAboveTitle = (TextView) findViewById(R.id.tv_above_title);
		mAboveTitle.setText("社区精选");
		imgMore = (ImageView) findViewById(R.id.imageview_above_more);
		imgMore.setOnClickListener(this);
		// above分页
		mText = (TextView) findViewById(R.id.above_text);
		// behind list
		lvTitle = (ListView) findViewById(R.id.behind_list_show);
		// behind button
		imgLogin = (ImageButton) findViewById(R.id.login_login);
		imgLogin.setOnClickListener(this);
		cbFeedback = (CustomButton) findViewById(R.id.cbFeedback);
		cbFeedback.setOnClickListener(this);
		cbAbove = (CustomButton) findViewById(R.id.cbAbove);
		cbAbove.setOnClickListener(this);
	}

	private void initListView() {
		behindListAdapter = new BehindListAdapter(this);
		lvTitle.setAdapter(behindListAdapter);
		lvTitle.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mAboveTitle.setText(behindListAdapter.getItem(position));
				mText.setText("position = " + position);
				toggle();
				behindListAdapter.setCurrentItem(position);
				behindListAdapter.notifyDataSetChanged();
				boolean hasPopAd = AppConnect.getInstance(MainActivity.this).hasPopAd(MainActivity.this);
				if(hasPopAd){
					AppConnect.getInstance(MainActivity.this).showPopAd(MainActivity.this);
					//根据指定的theme样式展示插屏广告，theme主要为系统样式id
					//AppConnect.getInstance(this).showPopAd(this, android.R.style.Theme_Translucent);
				}
			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Linear_above_toHome:
			if (sm.isMenuShowing()) {
				toggle();
			} else {
				showMenu();
			}
			break;
		case R.id.login_login:
			// 应用推荐
			//显示推荐列表（综合）
			AppConnect.getInstance(this).showOffers(this);
			break;
		case R.id.cbFeedback:
			boolean hasPopAd = AppConnect.getInstance(this).hasPopAd(this);
			if(hasPopAd){
				AppConnect.getInstance(this).showPopAd(this);
				//根据指定的theme样式展示插屏广告，theme主要为系统样式id
				//AppConnect.getInstance(this).showPopAd(this, android.R.style.Theme_Translucent);
			}
			// 精品
			// UMFeedbackService.openUmengFeedbackSDK(this);
			break;
		case R.id.cbAbove:
			// 关于
			// IntentUtil.start_activity(this, AboutActivity.class);
			AppConnect.getInstance(MainActivity.this).showOffers(MainActivity.this);
			break;
		case R.id.imageview_above_more:
			Log.e("----", "xxxxxxxxxxxxxxxxxxxxxxxx");
			//显示推荐列表（软件）
			AppConnect.getInstance(this).showAppOffers(this);
			break;
		}
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			LogUtil.v("退出程序");
			finish();
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (sm.isMenuShowing()) {
				toggle();
			} else {
				showMenu();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
